package controller.alarm;

import java.util.Calendar;

import object.alarm.Alarm;
import utility.alarm.SettingAlarm;
import utility.alarm.StringUlti;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import database.alarm.AlarmDelegate;

public class AlarmReceiver extends Activity implements OnClickListener{

	private Button btnTurnOff;
	private TextView time;
	private TextView dd;
	private TextView alarmWork;
	private Intent intent;
	private int idAlarm;
	private Bundle sendBundle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_receiver);
		
        Bundle receiveBundle = this.getIntent().getExtras();
        idAlarm = receiveBundle.getInt(SettingAlarm.send_receive);
        
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		String am_pm = StringUlti.getInstance().getAM_PM(c.get(Calendar.AM_PM));		
		String formatTime = StringUlti.getInstance().formatTime(StringUlti.getInstance().formatHour(hour), minute, am_pm);
		time = (TextView) findViewById(R.id.timeAlarmRecevier);
		time.setText(formatTime);
		
		dd = (TextView) findViewById(R.id.dateAlarmRecevier);
        dd.setText(DateFormat.format("E, MMMM dd, yyyy", c));
        
        if(AlarmActivity.dataSource==null){
        	AlarmActivity.dataSource = new AlarmDelegate(this);
        	AlarmActivity.dataSource.open();
        }                
        Alarm alarm = AlarmActivity.dataSource.getAlarm(idAlarm);
        alarmWork = (TextView) findViewById(R.id.txtAlarm);
        alarmWork.setText(alarm.getWork());
        try {
	        if(MusicListActivity.mMediaPlayer==null) MusicListActivity.mMediaPlayer = new MediaPlayer();
	        AlarmActivity.vibrator.vibrate(5000);
	        if(alarm.getAudio().contains("assets/")){
	        	String nameAudio = alarm.getAudio().replace("assets/", "");
	        	AssetFileDescriptor afd = this.getAssets().openFd(SettingAlarm.mediaPath+"/"+nameAudio);
	        	MusicListActivity.mMediaPlayer.setDataSource(afd.getFileDescriptor());
	        }else{
	        	MusicListActivity.mMediaPlayer.setDataSource(alarm.getAudio());
	        }
	        MusicListActivity.mMediaPlayer.prepare();
	        MusicListActivity.mMediaPlayer.start();
        } catch (Exception e) {	
        	e.printStackTrace();
    	}
		btnTurnOff = (Button) findViewById(R.id.btnTurnOff);
		btnTurnOff.setOnClickListener(this);
	}

	public void onClick(View v) {
		if(v==btnTurnOff){
			sendBundle = new Bundle();
			sendBundle.putInt("idAlarm", idAlarm);
			intent = new Intent(AlarmReceiver.this, TurnOff.class);
			intent.putExtras(sendBundle);
			AlarmReceiver.this.startActivity(intent);
			AlarmReceiver.this.finish();
		}
	}

}
